<?php

namespace IDock\Insurance\ZSIC\Request;

use IDock\Insurance\ZSIC\Request\AllZSICRequest;
use IDock\Insurance\ZSIC\Response\VehicleActualValueResponse;
use IDock\Insurance\ZSIC\ZSICEnums;
use IDock\Visitor\Premium\PremiumRelationVisitor;
use IDock\Visitor\Premium\PremiumVehicleVisitor;
use IDock\Visitor\VehicleActualValueVisitor;

/**
 * 车辆实际价值计算
 */
class VehicleActualValueRequest extends AllZSICRequest
{

    public $requestCode = 'vehicleActualValueQuery';

    protected $response = VehicleActualValueResponse::class;

    protected $urlTip = '车辆实际价值计算';

    /**
     * @var VehicleActualValueVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        /**@var $premiumVehicleVisitor PremiumVehicleVisitor **/
        $premiumVehicleVisitor = $this->visitor->argc['premiumVehicleVisitor'] ?? '';
        if(empty($premiumVehicleVisitor)){
            throw new \Exception("保行通提示：配置异常");
        }
        /**@var PremiumRelationVisitor $owner*/
        $owner = $this->visitor->argc['ownerInfo'] ?? [];
        $data = [
            'base' => [
                'quoteType' => '2',//2-关联出单
                'vciEffectiveDate' => $this->visitor->startDate,
                'tciEffectiveDate' => $this->visitor->startDate,
            ],
            'vehicle' => [
                'idType' => ZSICEnums::$certificateTypeCode[$owner->certificateTypeCode],//车主证件类型
                'idNumber' => $owner->certificateNo,//车主证件号
                'engineNo' => $premiumVehicleVisitor->engineNo,
                'frameNo' => $premiumVehicleVisitor->vin,
                'brandName' => $premiumVehicleVisitor->vehicleModel,
                'printBrandName' => '',
                'firstRegistrationDate' => $premiumVehicleVisitor->firstRegisterDate,
                'modelCode' => $premiumVehicleVisitor->vehicleModelCode,
                'purchasePrice' => $this->visitor->vehiclePrice,
                'seat' => $this->visitor->vehicleSeat,
                'capacity' => round(round($premiumVehicleVisitor->vehicleExhaust,3) * 1000,2),
                'importFlag' => $premiumVehicleVisitor->vehicleImport,
                'weight' => floatval($premiumVehicleVisitor->wholeWeight),
                'tonnage' => floatval($premiumVehicleVisitor->vehicleTonnage),
                'transferFlag' => intval($premiumVehicleVisitor->isTransfer),
                'transferDate' => $premiumVehicleVisitor->transferDate,
                'issueDate' => $premiumVehicleVisitor->issueDate,
                'vehicleType' => $this->getVehicleType($premiumVehicleVisitor),
                'businessType' => ZSICEnums::$useNatureCode[$premiumVehicleVisitor->useNatureCode] ?? '',
                'pmOriCode' => $premiumVehicleVisitor->vehicleStyleCode,
                'licenseColor' => ZSICEnums::$licenseColorCode[$premiumVehicleVisitor->licenseColorCode] ?? '',
                'ownershipType' => ZSICEnums::$attachNature[$premiumVehicleVisitor->attachNature] ?? '',
                'purchaseDate' => '',//purchaseDate
                'useType' => ZSICEnums::$causeType[$premiumVehicleVisitor->causeType] ?? '',
                'drivingArea' => '',
                'vehicleCategory' => ZSICEnums::$carKindCode[$premiumVehicleVisitor->carKindCode] ?? '',
                'loanFlag' => '0',
                'customerType' => $owner->typeCode == 'R1' ? '1' : '2',
                'licenseType' => ZSICEnums::$licenseTypeCode[$premiumVehicleVisitor->licenseTypeCode] ?? '',
                'fuelType' => ZSICEnums::$energyType[$premiumVehicleVisitor->energyTypeCode] ?? '',
            ],
        ];
        if($this->visitor->isNewEnergy){
            $data['base']['vciProductVersion'] = 'NE';//新能源
        }
        return $data;
    }

    public function getVehicleType(PremiumVehicleVisitor $premiumVehicleVisitor)
    {
        $code = '';
        $seat = $premiumVehicleVisitor->vehicleSeat;
        if($premiumVehicleVisitor->carKindCode == '200'){
            //货车
            $weight = round(round($premiumVehicleVisitor->vehicleTonnage,3) / 1000,2);

            if($weight <= 2){
                $code = '6';
            }elseif ($weight < 5){
                $code = '7';
            }elseif ($weight < 10){
                $code = '8';
            }elseif ($weight > 10){
                $code = '9';
            }
        }else{
            //客车
            if($seat <= 5){
                $code = '1';
            }elseif($seat < 10){
                $code = '2';
            }elseif($seat < 20){
                $code = '3';
            }elseif($seat < 36){
                $code = '4';
            }else{
                $code = '5';
            }
        }
        if(empty($code)){
            throw new \Exception("车辆种类不能为空，请检查荷载质量和座位数量是否正确");
        }
        return $code;
    }

    public function getBusinessNo()
    {
        return $this->visitor->argc['businessNo'] ?? '';
    }
}
